//
//  CNBBSPostedScoreViewController.m
//  CsdnPlus
//
//  Created by 孟瑞瑞 on 2018/6/7.
//  Copyright © 2018年 CSDN. All rights reserved.
//

#import "CNBBSPostedScoreViewController.h"
#import "CNBBSScoreCell.h"
#import "CNBBSFavoriteViewModel.h"
#import <Masonry/Masonry.h>
#import <CNBaseTool/CNCategoryHeader.h>

@interface CNBBSPostedScoreViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UILabel *avScoreLb;

@property (nonatomic, strong) CNBBSFavoriteViewModel *scoreVM;

@property (nonatomic, strong) NSArray *techScoreArray;

@property (nonatomic, strong) NSArray *scoreArray;

@end

@implementation CNBBSPostedScoreViewController

-(CNBBSFavoriteViewModel *)scoreVM
{
    if (!_scoreVM) {
        _scoreVM = [[CNBBSFavoriteViewModel alloc]init];
    }
    return _scoreVM;
}

-(UILabel *)avScoreLb
{
    if (!_avScoreLb) {
        _avScoreLb = [[UILabel alloc]init];
        _avScoreLb.theme_backgroundColor = searchPageBGColorPicker;
        _avScoreLb.theme_textColor = readAndTimeTextColorPicker;
        _avScoreLb.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:_avScoreLb];
        [_avScoreLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.mas_equalTo(0);
            make.height.mas_equalTo(32);
        }];
    }
    return _avScoreLb;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = YES;
        _tableView.theme_backgroundColor = searchPageBGColorPicker;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[CNBBSScoreCell class] forCellReuseIdentifier:@"Cell"];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.avScoreLb.mas_bottom).mas_equalTo(0);
            make.left.right.bottom.mas_equalTo(0);
        }];
            
        //去掉分割线
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        };
        
    }
    return _tableView;
}

-(NSArray *)scoreArray
{
    if (!_scoreArray) {
        _scoreArray = @[@"0",@"20",@"50",@"100",@"150",@"200",@"250",@"300",@"350",@"400"];
    }
    return _scoreArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self getScore];
    self.tableView.hidden = NO;
    self.title = @"给分数";
    [Factory addBlackItemToVC:self];

}

#pragma mark - 获取数据

-(void)getScore
{
    [self.scoreVM getBBSScoreCompletionHandle:^(NSError *error) {
        if (!error) {
            if (self.scoreVM.code == 200) {
                self.avScoreLb.text = [NSString stringWithFormat:@"可用分：%@",self.scoreVM.score];
            }
        }
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.techForum)
    {
        return self.scoreArray.count - 1;
    }
    return self.scoreArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CNBBSScoreCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    //配置Cell
    [self configureCell:cell atIndexPath:indexPath];
    
    return cell;
}


- (void)configureCell:(CNBBSScoreCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    //    cell.fd_enforceFrameLayout = NO; // Enable to use "-sizeThatFits:"
    if (self.techForum)
    {
        cell.titleLb.text = self.scoreArray[indexPath.row + 1];
    }
    else
    {
        cell.titleLb.text = self.scoreArray[indexPath.row];
    }
    
    cell.lineView.hidden = NO;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    return 44;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.techForum)
    {
        if ([self.scoreVM.score integerValue] < [self.scoreArray[indexPath.row + 1] integerValue])
        {
            [CSDNTool showAlert:@"积分不够！" inView:self.view];
        }
        else
        {
            self.scoreBlock(self.scoreArray[indexPath.row + 1]);
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else
    {
        if ([self.scoreVM.score integerValue] < [self.scoreArray[indexPath.row] integerValue])
        {
            [CSDNTool showAlert:@"积分不够！" inView:self.view]; 
        }
        else
        {
            self.scoreBlock(self.scoreArray[indexPath.row]);
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    
}

@end
